
package com.carsite.dao.hibernate;

import java.util.List;

import org.springframework.orm.ObjectRetrievalFailureException;

import com.carsite.dao.DepartmentDao;
import com.carsite.model.Department;

public class DepartmentDaoHibernate extends BaseDaoHibernate implements DepartmentDao {

    /**
     * @see com.carsite.dao.DepartmentDao#getDepartments(com.carsite.model.Department)
     */
    public List getDepartments(final Department department) {
        return getHibernateTemplate().find("from Department");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (department == null) {
            return getHibernateTemplate().find("from Department");
        } else {
            // filter on properties set in the department
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(department).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Department.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.carsite.dao.DepartmentDao#getDepartment(Long deptId)
     */
    public Department getDepartment(final Long deptId) {
        Department department = (Department) getHibernateTemplate().get(Department.class, deptId);
        if (department == null) {
            log.warn("uh oh, department with deptId '" + deptId + "' not found...");
            throw new ObjectRetrievalFailureException(Department.class, deptId);
        }

        return department;
    }

    /**
     * @see com.carsite.dao.DepartmentDao#saveDepartment(Department department)
     */    
    public void saveDepartment(final Department department) {
    	this.getSession().clear();
        getHibernateTemplate().saveOrUpdate(department);
    }

    /**
     * @see com.carsite.dao.DepartmentDao#removeDepartment(Long deptId)
     */
    public void removeDepartment(final Long deptId) {
        getHibernateTemplate().delete(getDepartment(deptId));
    }

	public List getDeptsByBranch(String branchId) {
		 return getHibernateTemplate().find("from Department where branchId ="+branchId+" order by branchId");
	}
	public List<Department> getWarehouse(){
		return (List<Department>)getHibernateTemplate().find("from Department where isWarehouse ="+true+" order by deptName");
	}
	
}
